<?php
namespace Home\Controller;
class NewsController extends HomeController {
	public function _initialize() {
        parent::_initialize();
        $this->_cate_mod = D('ArticleCate');
        $this->_mod = D('Article');
        $this->default_cate_id = 1;
    }
    public function _empty($title){
        $this->index(0,$title);
    }
    public function index($id = 0,$alias=''){
        $cate_id = empty($id)?I('id',0,'intval'):$id;
        if(!$cate_id && $alias){
            $cate_id = $this->_cate_mod->where(array('alias'=>$alias))->getField('id');
        }
        $cate_id = empty($cate_id)?$this->default_cate_id:$cate_id;
        (!$cate_id) && $this->error('参数有误');
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
        //子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->select();
            $big_cate = $cate;
        } else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
        }
        //分页
        $count = $this->_mod->where($map)->count();
        $Page = new \Think\Page($count,C('pagesize'));
        $list = $this->_mod->where($map)->field('id,title,add_time,intro,url')->limit($Page->firstRow.','.$Page->listRows)->relation('tag')->order('ordid desc,id desc')->select();
        $show = $Page->show();

        $this->assign('sub_cate',$sub_cate);
        $this->assign('cate',$cate);
        $this->assign('big_cate',$big_cate);
        $this->assign('list',$list);
        $this->assign('page',$show);
        $templet = empty($cate['index_templet'])?'index':$cate['index_templet'];
        $this->display($templet);
    }

    public function category($id = 0,$alias=''){
        $cate_id = empty($id)?I('id',0,'intval'):$id;
        if(!cate_id && $alias){
            $cate_id = $this->_cate_mod->where(array('alias'=>$alias))->getField('id');
        }
		$cate_id = empty($cate_id) ? $this->default_cate_id:$cate_id;
        (!$cate_id) && $this->error('参数有误');
		$info = $this->_mod->find($cate_id);
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
        //
		if($cate['type']){
			$this->category_page($cate_id);
			exit();
		}
        //子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->select();
            $big_cate = $cate;
        }else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
        }
        //分页
        $count = $this->_mod->where($map)->count();
        $Page = new \Think\Page($count,C('pagesize'));
        $list = $this->_mod->where($map)->field('id,title,add_time,intro,img,url')->limit($Page->firstRow.','.$Page->listRows)->relation('tag')->order('ordid desc,id desc')->select();
        $show = $Page->show();
	
		$this->assign('info',$info);
        $this->assign('sub_cate',$sub_cate);
        $this->assign('cate',$cate);
        $this->assign('big_cate',$big_cate);
        $this->assign('list',$list);
        $this->assign('page',$show);

        $templet = empty($cate['category_templet'])?'category':$cate['category_templet'];
        $this->display($templet);
    }

    public function activities($id = 18,$alias=''){
        //取推荐两条
        $top = D('Attr')->relation('top_active')->find(2);
		$top_ids = i_array_column($top['top_active'],'id');
		$cate_id = 18;
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
		//子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->select();
            $big_cate = $cate;
        }else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
        }
        //分页
        $list = $this->_mod->where($map)->field('id,title,add_time,intro,img,url')->order('ordid desc,id desc')->select();

		$this->assign('top',$top['top_active']);
		$this->assign('info',$info);
        $this->assign('sub_cate',$sub_cate);
        $this->assign('cate',$cate);
        $this->assign('big_cate',$big_cate);
        $this->assign('list',$list);
		$this->assign('top_ids',$top_ids);

        $templet = empty($cate['category_templet'])?'category':$cate['category_templet'];
        $this->display($templet);
    }

    public function detail(){
        $id = I('id',0,intval);
        (!$id) && $this->error('参数有误');
        $info = $this->_mod->find($id);
        $cate_id = $info['cate_id'];
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
        //子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->order('ordid desc,id desc')->select();
            $big_cate = $cate;
        }else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->order('ordid desc,id desc')->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);

        //前一篇和下一篇
        $map['status'] = array('eq',1);
        $map['cate_id'] = array('IN', $id_arr);

        $map['id'] = array('lt',$id);
        $ex = $this->_mod->field('id,title,url')->where($map)->order('id desc')->find();

        $map['id'] = array('gt',$id);
        $next = $this->_mod->field('id,title,url')->where($map)->order('id asc')->find();
		
		//相关链接
		$info['tags'] = tags_rule_search($info['tags']);
		$info['tags'] = '%'.$info['tags'].'%';
		$tags = explode(',',$info['tags']);
		$like = $this->_mod->field('id,title,url')->where(array('tags'=>array('like',$tags,'OR')))->limit(8)->select();

        $this->assign('cate',$cate);
        $this->assign('info',$info);
        $this->assign('ex',$ex);
        $this->assign('next',$next);
        $this->assign('sub_cate',$sub_cate);
        $this->assign('big_cate',$big_cate);
		$this->assign('like',$like);
        $this->display();
    }
	
	public function category_page($id=0){
		
        $cate_id = I('cate_id',0,'intval');
		$info_id = I('info_id',0,'intval');
        $cate_id = empty($cate_id) ? $id:$cate_id;
		if(empty($info_id)){
			$info_id = $this->_mod->where('cate_id='.$cate_id)->order('ordid desc')->getField('id');
		}
		
        $info = $this->_mod->find($info_id);
        //当前栏目信息
        $cate = $this->_cate_mod->find($cate_id);
        //子栏目列表
        if($cate['spid'] == 0){
            $sub_cate = $this->_cate_mod->where(array('pid'=>$cate_id))->order('ordid desc,id desc')->select();
            $big_cate = $cate;
        }else{
            $pid = current(explode('|',$cate['spid']));
            $sub_cate = $this->_cate_mod->where(array('pid'=>$pid))->order('ordid desc,id desc')->select();
            $big_cate = $this->_cate_mod->find($pid);
        }

        $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);

        //前一篇和下一篇
        $map['status'] = array('eq',1);
        $map['cate_id'] = array('IN', $id_arr);

        $map['id'] = array('lt',$info_id);
        $ex = $this->_mod->field('id,title')->where($map)->order('id desc')->find();

        $map['id'] = array('gt',$info_id);
        $next = $this->_mod->field('id,title')->where($map)->order('id asc')->find();

        $list = $this->_mod->field('id,title')->where(array('cate_id'=>$cate_id))->order('ordid desc,id desc')->select();

        $this->assign('list',$list);
        $this->assign('cate',$cate);
        $this->assign('info',$info);
        $this->assign('ex',$ex);
        $this->assign('next',$next);
        $this->assign('sub_cate',$sub_cate);
        $this->assign('$big_cate',$big_cate);
		
        $templet = empty($cate['category_templet'])?'category_page':$cate['category_templet'];
        $this->display($templet);
    }
	public function search(){  
    $keyword = $_GET['search'];  
//	$keyword= mb_convert_encoding($keyword,'GBK','UTF-8');
    $Goods=D('Article');  
    $map['id|title']  = array('like','%'.$keyword.'%');  
//	print_r($Goods);
	var_dump($map);
	echo $keyword;
//	var_dump($keyword);
    // 把查询条件传入查询方法  
    if($goods=$Goods->where($map)->select())  
     {
              $this->ajaxReturn($goods,'查询成功!',1);  
		  //iconv('GB2312', 'UTF-8', $goods); 
		  foreach($goods as $good){
		  	$good['title'] = mb_convert_encoding($good['title'],'GBK','UTF-8');
			  echo $good['title'];
		  }
//				$goods = mb_convert_encoding($goods,'GBK','UTF-8');
//     	print_r($goods);
     }else{
     	echo 22;
              $this->ajaxReturn($data,"查询失败,数据不存在！",0);  
     	print_r($data);
 }  
     	echo $Goods->_sql();
}
	public function test(){
		echo 'hello world';
	}
}